<?php

class IO_Page
{
	private $intNumberPagePerNavi = 5;/// so trang tren 1 phan trang
	private $intNumberRecordPerPage = 2;//so record ket qua tren 1 trang
	private $strFunctionOnclick = 'onPage';// ten function de goi javasctip khi click tren cac trang
	
	function IO_Page($intNumberPagePerNavi = 5, $intNumberRecordPerPage = 2, $strFunctionOnclick = 'onPage')
	{
		$this -> intNumberPagePerNavi = $intNumberPagePerNavi;
		$this -> intNumberRecordPerPage = $intNumberRecordPerPage;
		$this -> strFunctionOnclick = $strFunctionOnclick;
	}
	
	/*
	* Hàm tao phan trang
	* Type: Function <br />
	*	Name: createPageNavigator <br />
	*	Modify: Thach Le <thach.le@ionetwork.com> <br />
	*	Create date: Oct 12 08 <br />
	* Descript:
	* Param: 	$startPage: trang hien tai, 
	*					$total: Tong so records,
	*					
	*/		
	public function createPageNavigatorFront($startPage, $total, $attach = '')
	{			
		/// tinh so trang
		$strPage = "";
		$numberPage = ceil($total / $this->intNumberRecordPerPage);
		if ( $numberPage == 1 )
			return $strPage;
				
		// xac dinh trang dau, trang cuoi
		$start = $startPage;// truoc tien gan trang dau = trang hien tai
				
		if ( $start - ceil($this->intNumberPagePerNavi/2) <= 0 )
		{
			$start = 1;
		}
		else
		{
			if ( ($numberPage - $start) >= floor($this->intNumberPagePerNavi/2) )
				$start = $start - floor($this->intNumberPagePerNavi/2);
			else
			{
				$start = $numberPage - $this->intNumberPagePerNavi + 1;
				if ( $start <= 0 )
					$start = 1;
			}
		}
		
		if ( $numberPage >= $start + $this->intNumberPagePerNavi )
			$end = $start + $this->intNumberPagePerNavi - 1;
		else
			$end = $numberPage;
					
		for ( $i = $start; $i <= $end; $i++ )
		{
			if ( $i == $startPage )
			{
				$strHref = "<td width='10'></td><td><a class='link_page active_page'>$i</a></td>";
			}
			else
			{
				$strHref = "<td width='10'></td><td><a class='link_page' href='javascript:" . $this->strFunctionOnclick . "(" . $i . ",\"" . $attach . "\")'>";
				$strHref .= $i;
				$strHref .= "</a></td>";
			}
			
			$strPage .=  $strHref;
			/*if ( $i != $end )
				$strPage .= "&nbsp;&nbsp;";*/
		}
		
		if ( $startPage > 1 ) 
		{	
			$tmp = "<td width='10'></td><td><a class='link_page' href='javascript:" . $this->strFunctionOnclick . "(" . 1 . ",\"" . $attach . "\")'>";
			$tmp .= '&lt;&lt;';
			$tmp .= "</a></td>";
			$tmp .= "<td width='10'></td><td><a class='link_page' href='javascript:" . $this->strFunctionOnclick . "(" . ($startPage-1) . ",\"" . $attach . "\")'>";
			$tmp .= '&lt;';
			$tmp .= "</a></td>";
			$strPage = $tmp . $strPage;
			
		}
		
		if ( $startPage < $end )
		{
			$strPage .= "<td width='10'></td><td><a class='link_page' href='javascript:" . $this->strFunctionOnclick . "(" . ($startPage+1) . ",\"" . $attach . "\")'>&nbsp;";
			$strPage .= '&gt;';
			$strPage .= "&nbsp;</a></td>";
			
			$strPage .= "<td width='10'></td><td><a class='link_page' href='javascript:" . $this->strFunctionOnclick . "(" . $numberPage . ",\"" . $attach . "\")'>&nbsp;";
			$strPage .= '&gt;&gt;';
			$strPage .= "&nbsp;</a></td>";
		}
		return $strPage;		
	}
	
		
	/*
	* Hàm tao phan trang
	* Type: Function <br />
	*	Name: createPageNavigator <br />
	*	Modify: Thach Le <thach.le@ionetwork.com> <br />
	*	Create date: Oct 12 08 <br />
	* Descript:
	* Param: 	$startPage: trang hien tai, 
	*					$total: Tong so records,
	*					
	*/		
	public function createPageNavigator($startPage, $total, $attach = '')
	{			
		/// tinh so trang
		$strPage = "";
		$numberPage = ceil($total / $this->intNumberRecordPerPage);
		if ( $numberPage == 1 )
			return $strPage;
				
		// xac dinh trang dau, trang cuoi
		$start = $startPage;// truoc tien gan trang dau = trang hien tai
				
		if ( $start - ceil($this->intNumberPagePerNavi/2) <= 0 )
		{
			$start = 1;
		}
		else
		{
			if ( ($numberPage - $start) >= floor($this->intNumberPagePerNavi/2) )
				$start = $start - floor($this->intNumberPagePerNavi/2);
			else
			{
				$start = $numberPage - $this->intNumberPagePerNavi + 1;
				if ( $start <= 0 )
					$start = 1;
			}
		}
		
		if ( $numberPage >= $start + $this->intNumberPagePerNavi )
			$end = $start + $this->intNumberPagePerNavi - 1;
		else
			$end = $numberPage;
					
		for ( $i = $start; $i <= $end; $i++ )
		{
			if ( $i == $startPage )
			{
				$strHref = "[ <strong style='color:#FF0000'>$i</strong> ]";
			}
			else
			{
				$strHref = "[<a href='javascript:" . $this->strFunctionOnclick . "(" . $i . ",\"" . $attach . "\")'>&nbsp;";
				$strHref .= $i;
				$strHref .= "&nbsp;</a>]";
			}
			
			$strPage .=  $strHref;
			if ( $i != $end )
				$strPage .= "&nbsp;&nbsp;";
		}
		
		if ( $start > 1 ) 
		{	
			$tmp = "[<a href='javascript:" . $this->strFunctionOnclick . "(" . 1 . ",\"" . $attach . "\")'>&nbsp;";
			$tmp .= 1;
			$tmp .= "&nbsp;</a>]";
			$tmp .= "&nbsp;...&nbsp;";
			$strPage = $tmp . $strPage;
		}
		
		if ( $numberPage > $end )
		{
			$strPage .= "&nbsp;...&nbsp;";
			$strPage .= "[<a href='javascript:" . $this->strFunctionOnclick . "(" . $numberPage . ",\"" . $attach . "\")'>&nbsp;";
			$strPage .= $numberPage;
			$strPage .= "&nbsp;</a>]";
		}
		return $strPage;		
	}
}

?>